//
//  SingleProgrameViewController.m
//  花花时尚
//
//  Created by 霍文轩 on 15/8/27.
//  Copyright (c) 2015年 霍文轩. All rights reserved.
//

#import "SingleProgrameViewController.h"
#import "AsyncGETConnection.h"
#import "SinglePullLayout.h"
#import "DailyCollectionCell.h"
#import "ArticleModel.h"
#import "DailyDetailViewController.h"

#import "MJRefresh.h"
#import "NightMode.h"

@interface SingleProgrameViewController () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain) NSNumber * id;
@property (nonatomic,retain) UIView * downView;
@property (nonatomic,retain) NSMutableArray * collectionArray;
@property (nonatomic,assign) NSInteger currentStart;
@end

@implementation SingleProgrameViewController
- (void)dealloc
{
    [_downView release];
    [_collectionArray release];
    [super dealloc];
}
-(instancetype)initWithId:(NSNumber *)id{
    self = [super init];
    if(self){
        self.currentStart = 0;
        self.id = id;
        self.collectionArray = [NSMutableArray array];
        NSString * urlString = [NSString stringWithFormat:@"http://mrosevision.sinaapp.com/mfashion_api_v200.php?ac=getflow&type=3&id=%@&did=640%%2C1136%%2CiPhone5%%2C2&dim=02%%3A00%%3A00%%3A00%%3A00%%3A00&format=json&dic=AppStore&div=2.1.0&djb=1&count=50&diu=0F607264FC6318A92B9E13C65DB7CD3C",id];
        [AsyncGETConnection asyncConnectionWithURLString:urlString parmaters:nil block:^(NSData *data) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            _currentStart = 50;
            
            for (NSDictionary * subDic in [dic objectForKey:@"article"]) {
                ArticleModel * model = [[ArticleModel alloc] init];
                [model setValuesForKeysWithDictionary:subDic];
                [self.collectionArray addObject:model];
                [model release];
            }
            [self createCollection];
        }];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    
    [self createDownView];
}
-(void)createDownView{
    self.downView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49)];
    UIImage * detailDownImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"detaildown":@"detaildown_light" ofType:@"png"]];
    _downView.backgroundColor = [UIColor colorWithPatternImage:detailDownImage];
    [self.view addSubview:_downView];
    [_downView release];
    
    UIButton * returnButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 10, 30, 30)];
    UIImage * returnButtonImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dailyreturn" ofType:@"png"]];
    returnButton.backgroundColor = [UIColor colorWithPatternImage:returnButtonImage];
    [returnButton addTarget:self action:@selector(returnButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_downView addSubview:returnButton];
    [returnButton release];
}
-(void)createCollection{
    SinglePullLayout * layout = [[[SinglePullLayout alloc] init] autorelease];
    layout.dicArr = _collectionArray; // 要内存相等！！！！！
    
    UICollectionView * collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20-49) collectionViewLayout:layout];
    collection.delegate = self;
    collection.dataSource = self;
    collection.backgroundColor = [UIColor clearColor];
    [collection registerClass:[DailyCollectionCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:collection];
    
    [collection addHeaderWithCallback:^{
        NSString * urlString = [NSString stringWithFormat:@"http://mrosevision.sinaapp.com/mfashion_api_v200.php?ac=getflow&type=3&id=%@&did=640%%2C1136%%2CiPhone5%%2C2&dim=02%%3A00%%3A00%%3A00%%3A00%%3A00&format=json&dic=AppStore&div=2.1.0&djb=1&count=50&diu=0F607264FC6318A92B9E13C65DB7CD3C",_id];
        [AsyncGETConnection asyncConnectionWithURLString:urlString parmaters:nil block:^(NSData *data) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            _currentStart = 50;
            
            [self.collectionArray removeAllObjects];
            
            for (NSDictionary * subDic in [dic objectForKey:@"article"]) {
                ArticleModel * model = [[ArticleModel alloc] init];
                [model setValuesForKeysWithDictionary:subDic];
                [self.collectionArray addObject:model];
                [model release];
            }
            [collection reloadData];
            [collection headerEndRefreshing];
        }];
    }];
    [collection headerBeginRefreshing];
    
    [collection addFooterWithCallback:^{
        NSString * urlString = [NSString stringWithFormat:@"http://mrosevision.sinaapp.com/mfashion_api_v200.php?ac=getflow&id=%@&dim=02%%3A00%%3A00%%3A00%%3A00%%3A00&type=3&dic=AppStore&diu=0F607264FC6318A92B9E13C65DB7CD3C&start=%ld&djb=1&did=640%%2C1136%%2CiPhone5%%2C2&div=2.1.0&count=50&format=json",_id,(long)_currentStart];
        [AsyncGETConnection asyncConnectionWithURLString:urlString parmaters:nil block:^(NSData *data) {
            NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            _currentStart += 50;

            for (NSDictionary * subDic in [dic objectForKey:@"article"]) {
                ArticleModel * model = [[ArticleModel alloc] init];
                [model setValuesForKeysWithDictionary:subDic];
                [self.collectionArray addObject:model];
                [model release];
            }
            [collection reloadData];
            [collection footerEndRefreshing];
        }];
    }];
    
    [collection release];
    
    [self.view addSubview:_downView];
}
#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DailyCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ArticleModel * model = [_collectionArray objectAtIndex:indexPath.item];
    [cell setCellWithModel:model];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    ArticleModel * model = [_collectionArray objectAtIndex:indexPath.item];
    [self enterDetailWithModel:model];
}
#pragma mark - DailyHeader协议
-(void)enterDetailWithModel:(ArticleModel *)model{
    DailyDetailViewController * detail = [[DailyDetailViewController alloc] initWithModel:model];
    [self presentViewController:detail animated:YES completion:^{
        
    }];
    [detail release];
}
-(void)returnButtonClick{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return [NightMode nightModeON]?UIStatusBarStyleLightContent:UIStatusBarStyleDefault;
}

@end
